Skip to content

Conversation

@ephraimbuddy
Copy link
Contributor

In HA, two scheduler processes can race to schedule the same TaskInstance. Previously DagRun.schedule_tis() updated rows by ti.id alone, so a scheduler could increment try_number and transition state even after another scheduler had already advanced the TI (e.g. to SCHEDULED/QUEUED), resulting in duplicate attempts being queued.

This change makes scheduling idempotent under HA races by:

  • Guarding schedule_tis() DB updates to only apply when the TI is still in schedulable states (derived from SCHEDULEABLE_STATES, handling NULL explicitly).

  • Using a single CASE (next_try_number) so reschedules (UP_FOR_RESCHEDULE) do not start a new try, and applying this consistently to both normal scheduling and the EmptyOperator fast-path.

Adds regression tests covering:

  • TI already queued by another scheduler.
  • EmptyOperator fast-path blocked when TI is already QUEUED/RUNNING.
  • UP_FOR_RESCHEDULE scheduling keeps try_number unchanged.
  • Only one “scheduler” update succeeds when competing.

Closes: #57618

Note: The reproduction of this issue was based on unit tests

In HA, two scheduler processes can race to schedule the same
TaskInstance. Previously DagRun.schedule_tis() updated rows by ti.id
alone, so a scheduler could increment try_number and transition
state even after another scheduler had already advanced the TI (e.g. to
SCHEDULED/QUEUED), resulting in duplicate attempts being queued.

This change makes scheduling idempotent under HA races by:
- Guarding schedule_tis() DB updates to only apply when the TI is still
in schedulable states (derived from SCHEDULEABLE_STATES, handling
NULL explicitly).

- Using a single CASE (next_try_number) so reschedules
(UP_FOR_RESCHEDULE) do not start a new try, and applying this
consistently to both normal scheduling and the EmptyOperator fast-path.

Adds regression tests covering:
- TI already queued by another scheduler.
- EmptyOperator fast-path blocked when TI is already QUEUED/RUNNING.
- UP_FOR_RESCHEDULE scheduling keeps try_number unchanged.
- Only one “scheduler” update succeeds when competing.
Comment on lines +2070 to +2073
schedulable_state_clause = or_(
TI.state.is_(None),
TI.state.in_(non_null_schedulable_states),
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should do some benchmarking/checking of indexes on this query if you haven't already. TI state is almost certainly indexed, but we should check this with some EXPLAIN ANALYZE in a moderatly sized DB if we have one (something to the region of 1-2m TI rows?)

Might also be worth checking if a schedulable_state_clause = TI.state != TaskInstanceState.SCHEDULED is enough and more-performant?

Copy link
Member

@ashb ashb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM once we've done some perf testing

@dabla
Copy link
Contributor

dabla commented Jan 14, 2026

@ephraimbuddy Good job, we are actually testing this fix right now, keep you guys posted if it helped. The weird thing is it seems (we're not sure yet) that we only have this issue with DAG's that have tasks that use the WinRMOperator.

@dabla
Copy link
Contributor

dabla commented Jan 14, 2026

@ephraimbuddy Unfortunately, even after applying this fix, we still run onto the same issue.

@ephraimbuddy
Copy link
Contributor Author

@ephraimbuddy Unfortunately, even after applying this fix, we still run onto the same issue.

Major problem with the issue is reliability of reproduction path. I have reproduced it once but trying again, I can't. Do you have steps for reliable reproduction?

@dabla dabla mentioned this pull request Jan 16, 2026
1 task
@dabla
Copy link
Contributor

dabla commented Jan 21, 2026

Now we also experience the same issue with HttpOperator:

[2026-01-21 04:44:19] INFO - Calling HTTP method
[2026-01-21 04:44:19] INFO - The hook_class 'airflow.providers.http.hooks.http.HttpHook' is not fully initialized (UI widgets will be missing), because the 'flask_appbuilder' package is not installed, however it is not required for Airflow components to work
[2026-01-21 04:44:25] ERROR - Server indicated the task shouldn't be running anymore. Terminating process detail={"detail":{"reason":"not_found","message":"Task Instance not found"}}
[2026-01-21 04:44:30] ERROR - Task killed!

@eladkal eladkal added this to the Airflow 3.1.7 milestone Jan 21, 2026
@eladkal eladkal added the backport-to-v3-1-test Mark PR with this label to backport to v3-1-test branch label Jan 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-to-v3-1-test Mark PR with this label to backport to v3-1-test branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Schedulers race condition when using with kubernetes executor

4 participants